package com.winter.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.IAnnMapper;
import com.winter.mapper.IApplyMapper;
import com.winter.mapper.IAttaMapper;
import com.winter.mapper.NewCollectMapper;
import com.winter.mapper.NewExpertForumMapper;
import com.winter.mapper.NewGossipCommentMapper;
import com.winter.mapper.NewGossipLikeMapper;
import com.winter.mapper.NewGossipMapper;
import com.winter.mapper.NewOrgMapper;
import com.winter.mapper.NewOrgReportMapper;
import com.winter.mapper.NewResourceMapper;
import com.winter.mapper.NewTradeSalonMapper;
import com.winter.mapper.TExpertMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.FinContentLikeInfo;
import com.winter.model.IAnn;
import com.winter.model.IApply;
import com.winter.model.IAtta;
import com.winter.model.NewCollect;
import com.winter.model.NewExpertForum;
import com.winter.model.NewGossip;
import com.winter.model.NewGossipComment;
import com.winter.model.NewGossipLike;
import com.winter.model.NewOrg;
import com.winter.model.NewOrgReport;
import com.winter.model.NewResource;
import com.winter.model.NewTradeSalon;
import com.winter.model.TExpert;
import com.winter.model.UserInfo;
import com.winter.model.vo.response.IAnnChildResponseVo;
import com.winter.util.Constants;
import com.winter.util.P2pPubParaConstants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
@Transactional
public class ThinkTankService {

    @Autowired
    private TExpertMapper tExpertMapper;

    @Autowired
    private IAnnMapper iAnnMapper;

    @Autowired
    private IAttaMapper iAttaMapper;

    @Autowired
    private NewExpertForumMapper newExpertForumMapper;

    @Autowired
    private NewTradeSalonMapper newTradeSalonMapper;

    @Autowired
    private NewOrgMapper newOrgMapper;

    @Autowired
    private NewOrgReportMapper newOrgReportMapper;

    @Autowired
    private NewGossipMapper newGossipMapper;

    @Autowired
    private NewGossipLikeMapper newGossipLikeMapper;

    @Autowired
    private NewGossipCommentMapper newGossipCommentMapper;

    @Autowired
    private NewCollectMapper newCollectMapper;

    @Autowired
    private IApplyMapper iApplyMapper;

    @Autowired
    private NewResourceMapper newResourceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;

    /**
     * 专家
     */
    public Result specialistList(String key, Integer pageSize, Integer pageNumber) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<TExpert> tExperts = tExpertMapper.specialistList(key);
        PageInfo<TExpert> pageInfo = new PageInfo<>(tExperts);
        return Result.success(pageInfo);
    }

    /**
     * 高级论坛列表information
     */
    public Result highForumList(String keyword, Integer pageSize, Integer pageNumber, String type, String key) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<IAnnChildResponseVo> iAnnChildResponseVos = iAnnMapper.highForumList(keyword, type, key);
        if (iAnnChildResponseVos.size() > 0) {
            iAnnChildResponseVos.forEach(f -> {
                f.setIsExpire("0");
                Date holdTimeEnd = f.getHoldTimeEnd();
                if (holdTimeEnd != null) {
                    if (System.currentTimeMillis() > holdTimeEnd.getTime()) {
                        f.setIsExpire("0");
                    } else {
                        f.setIsExpire("1");
                    }
                }
                List<IAtta> iAtta = iAttaMapper.getIAtta(Long.parseLong(f.getId()), 13);
                if (iAtta.size() > 0) {
                    f.setIsUploading("1");
                } else {
                    f.setIsUploading("0");
                }
            });
        }
        PageInfo<IAnnChildResponseVo> pageInfo = new PageInfo<>(iAnnChildResponseVos);
        return Result.success(pageInfo);
    }

    /**
     * 高级论坛详情
     */
    public Result highForumDetails(Long id, String userId) {
        IAnn iAnn = iAnnMapper.selectByPrimaryKey(id);
        List<IApply> iApplies = Lists.newArrayList();
        if (iAnn != null) {
            if (!Strings.isNullOrEmpty(userId)) {
                String annType = iAnn.getAnnType();
                NewCollect newCollect = null;
                //  1 精品课程
                if (annType.equals("1")) {
                    // 查看是否被收藏
                    newCollect = newCollectMapper.selectCollect(iAnn.getId() + "", 4, Long.valueOf(userId));
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "1");
                }
                // 2同业
                if (annType.equals("4")) {
                    newCollect = newCollectMapper.selectCollect(iAnn.getId() + "", 5, Long.valueOf(userId));
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "4");
                }
                //4专家讲坛
                if (annType.equals("2")) {
                    newCollect = newCollectMapper.selectCollect(iAnn.getId() + "", 3, Long.valueOf(userId));
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "2");
                }
                // 3 高级论坛
                if (annType.equals("3")) {
                    newCollect = newCollectMapper.selectCollect(iAnn.getId() + "", 2, Long.valueOf(userId));
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "3");
                }
                if (newCollect != null) {
                    iAnn.setIsCollect("1");
                } else {
                    iAnn.setIsCollect("0");
                }

            } else {
                String annType = iAnn.getAnnType();
                //  1 精品课程
                if (annType.equals("1")) {
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "1");
                }
                //2 专家讲坛
                if (annType.equals("2")) {
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "2");
                }
                //4 同业专家讲坛
                if (annType.equals("4")) {
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "4");
                }
                // 3 高级论坛
                if (annType.equals("3")) {
                    // 查看报名总人数
                    iApplies = iApplyMapper.queryByIdAndType(iAnn.getId(), "3");
                }
                iAnn.setIsCollect("0");
            }
            // 报名总人数
            iAnn.setApplyNum(iApplies.size());
            List<IAtta> iAtta = iAttaMapper.getIAtta(id, P2pPubParaConstants.AttaType.LUNT_DOC);
            iAnn.setiAtta(iAtta);
            return Result.success(iAnn);
        }
        return Result.failure(ResultCode.DATA_NOT_EXIST);
    }

    /**
     * 高端论坛
     */
    public Result applySeniorForum(NewExpertForum newExpertForum) {
        if (newExpertForum == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newExpertForum.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(newExpertForum.getApplyName())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入您的姓名");
        }
        if (Strings.isNullOrEmpty(newExpertForum.getApplyPhone())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入您的联系电话");
        }
        if (newExpertForum.getApplyNum() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请选择参会人数");
        }
        newExpertForum.setId(UUID.randomUUID()
                                 .toString());
        newExpertForum.setCreatedAt(new Date());
        newExpertForum.setUpdatedAt(new Date());

        int i = newExpertForumMapper.insertSelective(newExpertForum);
        if (i > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "失败");
    }

    /**
     * 报名同业沙龙
     */
    public Result applyTradeSalon(NewTradeSalon newTradeSalon) {
        if (newTradeSalon == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newTradeSalon.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(newTradeSalon.getApplyName())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入您的姓名");
        }
        if (Strings.isNullOrEmpty(newTradeSalon.getApplySex())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择您的性别");
        }
        if (Strings.isNullOrEmpty(newTradeSalon.getApplyPhone())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入您的联系电话");
        }
        if (newTradeSalon.getApplyNum() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择参会人数");
        }
        newTradeSalon.setId(UUID.randomUUID()
                                .toString());
        newTradeSalon.setCreatedAt(new Date());
        newTradeSalon.setUpdatedAt(new Date());
        int i = newTradeSalonMapper.insertSelective(newTradeSalon);
        if (i == 1) {
            return Result.success();
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "报名失败");
    }

    /**
     * 申请入驻机构
     */
    public Result insertOrg(NewOrg newOrg) {
        if (newOrg == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newOrg.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgLogo())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请上传机构logo");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgPicture())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请上传团队照片");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgName())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入机构名称");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgContactName())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入联系人名称");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgContactPhone())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入联系人联系方式");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgPartiesName())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入负责人姓名");
        }
        if (Strings.isNullOrEmpty(newOrg.getOrgIntroduce())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入团队及负责人介绍");
        }
        newOrg.setCreatedAt(new Date());
        newOrg.setUpdatedAt(new Date());
        newOrg.setIsAudit("3");
        newOrg.setId(UUID.randomUUID()
                         .toString());
        int i = newOrgMapper.insertSelective(newOrg);
        if (i > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "入驻失败");
    }

    /**
     * 机构列表
     */
    public Result orgList(Integer pageSize, Integer pageNumber, String key) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewOrg> newOrg = newOrgMapper.orgList(key);
        newOrg.forEach(f -> {
                    List<NewOrgReport> newOrgReports = newOrgReportMapper.orgReportList(f.getId(), null, null);
                    f.setCountReport(newOrgReports.size() + "");
                }
        );
        PageInfo<NewOrg> pageInfo = new PageInfo<>(newOrg);
        return Result.success(pageInfo);
    }

    /**
     * 机构 研报
     */
    public Result orgReportList(Integer pageSize, Integer pageNumber, String orgId, String userId, String id, String key) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewOrgReport> newOrgReports = newOrgReportMapper.orgReportList(orgId, id, key);
        newOrgReports.forEach(f -> {
         /*   // 查看是否收藏过
            if (userId != null) {
                NewCollect newCollect = newCollectMapper.selectCollect(f.getId() + "", 1, Long.valueOf(userId));
                if (newCollect != null) {
                    f.setIsCollect("1");
                } else {
                    f.setIsCollect("0");
                }
            } else {
                f.setIsCollect("0");
            }*/
            // 机构信息
            NewOrg newOrg = newOrgMapper.selectByPrimaryKey(f.getOrgId());
            f.setNewOrg(newOrg);
       /*     // 附件信息
            List<IAtta> orgReportList = iAttaMapper.getIAtta(f.getId(), 100);
            f.setiAtta(orgReportList);*/
        });
        PageInfo<NewOrgReport> pageInfo = new PageInfo<>(newOrgReports);
        return Result.success(pageInfo);
    }

    public Result orgReportDetail(Long id, Long userId, String userNo) {
        NewOrgReport newOrgReport = newOrgReportMapper.selectByPrimaryKey(id);
        if (newOrgReport.getViewhits() == null) {
            newOrgReport.setViewhits(1L);
        } else {
            newOrgReport.setViewhits(newOrgReport.getViewhits() + 1);
        }

        NewOrgReport newOrgReport1 = new NewOrgReport();
        newOrgReport1.setId(id);
        newOrgReport1.setViewhits(newOrgReport.getViewhits() + 1);
        // 修改浏览数
        newOrgReportMapper.updateByPrimaryKeySelective(newOrgReport1);
        if (newOrgReport != null) {
            // 查看是否收藏过
            if (userId != null && userNo != null) {
                NewCollect newCollect = newCollectMapper.selectCollect(newOrgReport.getId() + "", 1, Long.valueOf(userId));
                if (newCollect != null) {
                    newOrgReport.setIsCollect("1");
                } else {
                    newOrgReport.setIsCollect("0");
                }
                List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(newOrgReport.getId(), "9", "1", userNo);
                if (isLike.size() > 0) {
                    newOrgReport.setIsLike("1");
                } else {
                    newOrgReport.setIsLike("0");
                }
            } else {
                newOrgReport.setIsLike("0");
                newOrgReport.setIsCollect("0");
            }
            // 附件信息
            List<IAtta> orgReportList = iAttaMapper.getIAtta(newOrgReport.getId(), 17);
            newOrgReport.setiAtta(orgReportList);
        }
        return Result.success(newOrgReport);
    }

    /**
     * b
     * 发布
     */
    public Result insertGossip(NewGossip newGossip) {
        if (newGossip == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newGossip.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (newGossip.getContent() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入comment八卦内容");
        }
        newGossip.setId(UUID.randomUUID()
                            .toString());
        newGossip.setCreatedAt(new Date());
        newGossip.setAudit("1");
        int i = newGossipMapper.insertSelective(newGossip);
        if (i > 0) {
            return Result.success(ResultCode.SUCCESS, "成功，等待审核");
        }
        return Result.failure(ResultCode.FAILURE, "发布失败");
    }

    /**
     * 点赞八卦
     */
    public Result likeGossip(NewGossipLike newGossipLike) {
        if (newGossipLike == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newGossipLike.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(newGossipLike.getGossipId())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择八卦");
        }
        newGossipLike.setCreatedAt(new Date());
        newGossipLike.setId(UUID.randomUUID()
                                .toString());
        NewGossipLike byUserAndGossip = newGossipLikeMapper.getByUserAndGossip(newGossipLike.getGossipId(), newGossipLike.getUserId()
                                                                                                                         .longValue());
        if (byUserAndGossip != null) {
            return Result.success(ResultCode.FAILURE, "你已经点赞过了");
        } else {
            int i = newGossipLikeMapper.insertSelective(newGossipLike);
            if (i > 0) {
                return Result.success(ResultCode.SUCCESS, "点赞成功");
            }
            return Result.failure(ResultCode.FAILURE, "点赞失败");
        }
    }

    /**
     * 取消点赞八卦
     */
    public Result cancelGossip(String gossipId, Long userId) {
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        int i = newGossipLikeMapper.cancelGossip(gossipId, userId);
        if (i > 0) {
            return Result.success(ResultCode.SUCCESS, "取消成功");
        }
        return Result.success(ResultCode.FAILURE, "取消失败");
    }

    /**
     * 八卦列表
     */
    public Result gossipList(Integer pageSize, Integer pageNumber, Long userId, Integer type, String keyword) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }

        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewGossip> newGossips = newGossipMapper.gossipList(keyword);
        newGossips.forEach(f -> {
            builderGossip(userId, f);
            countLike(f);
        });
        PageInfo<NewGossip> pageInfo = new PageInfo<>(newGossips);
        return Result.success(pageInfo);
    }


    /**
     * 详情
     */
    public Result gossipDetail(String gossipId, Long userId) {
        if (Strings.isNullOrEmpty(gossipId)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "八卦id不能为空");
        }
        // 修改浏览总数
        newGossipMapper.updateBrowseNum(gossipId);
        NewGossip newGossip = newGossipMapper.selectByPrimaryKey(gossipId);
        if (newGossip != null) {

            builderGossip(userId, newGossip);

            // 点赞总数
            countLike(newGossip);
        }
        return Result.success(newGossip);
    }

    /**
     * 八卦信息
     */
    private void builderGossip(Long userId, NewGossip newGossip) {
        if (userId != null) {
            //  是否有点赞
            NewGossipLike byUserAndGossip = newGossipLikeMapper.getByUserAndGossip(newGossip.getId(), userId);
            if (byUserAndGossip != null) {
                newGossip.setIsLike(1);
            } else {
                newGossip.setIsLike(0);
            }
            // 是否收藏  6 = 八卦
            NewCollect newCollect = newCollectMapper.selectUserCollect(newGossip.getId(), userId, 6);
            if (newCollect != null) {
                newGossip.setIsCollect(1);
            } else {
                newGossip.setIsCollect(0);
            }
        } else {
            newGossip.setIsLike(0);
            newGossip.setIsCollect(0);
        }
    }

    /**
     * 统计点赞/ 评论/收藏
     */
    private void countLike(NewGossip f) {
        // 点赞总数
        List<NewGossipLike> likes = newGossipLikeMapper.getByGossipId(f.getId());
        f.setLikeCount(likes.size());
        // 评论总数
        List<NewGossipComment> comments = newGossipCommentMapper.getByGossipId(f.getId());
        f.setCommentCount(comments.size());
        //newCollectMapper.get(f.getId(), 6);
    }

    /**
     * 删除
     */
    public Result deleteGossip(String gossipId, Long userId) {
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        NewGossip newGossip = newGossipMapper.selectByPrimaryKey(gossipId);
        if (userId.equals(newGossip.getUserId()
                                   .longValue())) {
            newGossipMapper.deleteByPrimaryKey(newGossip.getId());
            newCollectMapper.deleteByGossip(newGossip.getId(), Constants.GOSSIP_TYPE);
        } else {
            return Result.failure(ResultCode.FAILURE, "不能删除他人八卦信息");

        }
        return Result.success();
    }

    /**
     * 上线 || 下线
     */
    public Result isShow(String gossipId, Long userId, Integer type) {
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        Integer show = newGossipMapper.isShow(gossipId, type);
        if (show == 1) {
            return Result.success();
        } else {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "修改失败");
        }
    }


    /**
     * 评论||回复八卦
     */
    public Result commentGossip(NewGossipComment newGossipComment) {
        if (newGossipComment == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (newGossipComment.getUserId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        newGossipComment.setCreatedAt(new Date());
        newGossipComment.setId(UUID.randomUUID()
                                   .toString());
        int i = newGossipCommentMapper.insertSelective(newGossipComment);
        if (i > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.FAILURE, "失败");
    }

    /**
     * 收藏
     */
    public Result collect(NewCollect newCollect) {
        if (newCollect == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (Strings.isNullOrEmpty(newCollect.getUserId())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(newCollect.getSourceId())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择你要收藏的信息");
        }
        newCollect.setId(UUID.randomUUID()
                             .toString());
        newCollect.setCreatedAt(new Date());
        int i = newCollectMapper.insertSelective(newCollect);
        if (i == 1) {
            return Result.success();
        }
        return Result.failure(ResultCode.FAILURE, "失败");
    }

    public Result cancelCollect(String sourceId, String type, Long userId) {
        if (sourceId != null && type != null && userId != null) {
            newCollectMapper.deleteBySourceIdAndType(sourceId, type, userId);
            return Result.success();
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK);
    }

    /**
     * 报名
     */
    public Result apply(IApply iApply) {
        if (iApply.getRelaId() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "id不能为空");
        }
        if (iApply == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (Strings.isNullOrEmpty(iApply.getRelaUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        IApply isApply = iApplyMapper.getIsApply(iApply.getRelaId(), iApply.getRelaChildType(), iApply.getRelaUser());
        if (isApply != null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "你已经报过名啦");
        }
        iApply.setCreatetime(new Date());
        iApply.setUpdatetime(new Date());
        iApply.setUpdateUser(iApply.getRelaUser());
        iApply.setCreateuser(iApply.getRelaUser());
        iApply.setIsvalid("1");
        int i = iApplyMapper.insertSelective(iApply);
        if (i > 0) {
            return Result.success("报名成功");
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "报名失败");
    }


    /**
     * 课件列表
     */
    public Result courseWare(Integer type, String keyword, Integer pageSize, Integer pageNumber) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewResource> newResources = newResourceMapper.courseWareList(type, keyword);
        PageInfo<NewResource> pageInfo = new PageInfo<>(newResources);
        return Result.success(pageInfo);
    }


    /**
     * 详情
     */
    public Result orgDetail(String id) {
        if (id != null) {
            NewOrg newOrg = newOrgMapper.selectByPrimaryKey(id);
            return Result.success(newOrg);
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK);
    }

    /**
     * 八卦评论
     */
    public Result gossipComment(String gossipId, Integer pageSize, Integer pageNumber) {

        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewGossipComment> comments = newGossipCommentMapper.getByGossipId(gossipId);
        comments.forEach(f -> {

                    UserInfo userInfo = userInfoMapper.selectByPrimaryKey(f.getUserId()
                                                                           .longValue());
                    f.setUserPicture(userInfo.getCustImgUrl());
                    NewGossipComment byReply = newGossipCommentMapper.getByReply(f.getId());
                    UserInfo replyUserInfo = userInfoMapper.selectByPrimaryKey(f.getUserId()
                                                                                .longValue());
                    System.out.println(replyUserInfo.getCustImgUrl());
                    if (null != byReply) {
                        byReply.setUserPicture(replyUserInfo.getCustImgUrl());
                        f.setGossipRevert(byReply);
                    }
                }
        );
        PageInfo<NewGossipComment> info = new PageInfo<>(comments);
        return Result.success(info);
    }

    /**
     * 修改报名
     */
    public Result update(IApply iApply) {
        if (iApply.getId() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "id不能为空");
        }
        if (Strings.isNullOrEmpty(iApply.getRelaUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        iApply.setUpdatetime(new Date());
        iApply.setUpdateUser(iApply.getRelaUser());
        int i = iApplyMapper.updateByPrimaryKeySelective(iApply);
        if (i > 0) {
            return Result.success("修改成功");
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "修改失败");
    }

}
